/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
Components.utils.import("resource://gre/modules/PluralForm.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource:///modules/mailServices.js");
Components.utils.import("resource:///modules/folderUtils.jsm");
Components.utils.import("resource:///modules/iteratorUtils.jsm");
Components.utils.import("resource:///modules/MailUtils.js");
Components.utils.import("resource://gre/modules/DbTerminal.jsm");

var gDeviceWindowObj=null;
var gDeviceWindowObj1=null;
var gAccountWindowObj=null;

function checkOneAccount(arguement,arguement1)
{
 
   // alert("执行我:"+arguement);
   
    if(gAccountWindowObj === null || gAccountWindowObj.closed){  
        gAccountWindowObj = window.openDialog(
                "chrome://messenger/content/checkAccountInfo.xul", 
                "accountInfo",
                "chrome, dialog, centerscreen",arguement,arguement1);
    }else{
        gAccountWindowObj.close();
    }
    return false;
}

function checkAllDevice()
{
 
   // alert("执行我:"+arguement);
   
    if(gDeviceWindowObj === null || gDeviceWindowObj.closed){  
        gDeviceWindowObj = window.openDialog(
                "chrome://messenger/content/lookdeviceInfo.xul", 
                "deviceInfoWindow",
                "chrome, dialog, centerscreen");
    }else{
        gDeviceWindowObj.close();
    }
    return false;
}

function checkOneDevice(arguement)
{
 
   
    if(gDeviceWindowObj1 === null || gDeviceWindowObj1.closed){  
        gDeviceWindowObj1 = window.openDialog(
                "chrome://messenger/content/checkOneDevice.xul", 
                "deviceInfo",
                "chrome, dialog, centerscreen",arguement);
    }else{
        gDeviceWindowObj1.close();
    }
    return false;
}

function logoutDevice1(deviceName)
{
    var gEN = Components.classes["@nfs.hlj/upnpservice;1"].createInstance(Components.interfaces.nsIUpnpService);
    dDeviceSqlite.onLoad();
    var statement = dDeviceSqlite.dbConnection.createStatement("\
              UPDATE td_device SET dStatus = 0, dHandleStatus = 0 where dName = :myVal");
    statement.params.myVal =deviceName;
    try{
            while(statement.executeStep()){
               continue;
           }
       }
    finally{
        statement.reset();
    }
    var locationStr;
    statement = dDeviceSqlite.dbConnection.createStatement("\
          SELECT dId,dLocation FROM td_device WHERE dName = :myVal"); 
    statement.params.myVal =deviceName;
    var deviceId = null;
    try{
            while(statement.executeStep()){
                deviceId = statement.row.dId;
                locationStr=statement.row.dLocation;
               continue;
           }
       }
    finally{
        statement.reset();
    }
    var httpHead = locationStr.split("/")[0];
    var ipAndPort = locationStr.split("/")[2];
    var serviceSubStr =httpHead+"\/\/"+ipAndPort+"/upnp/control/tvcontrol1";
    gEN.SendAction(serviceSubStr,"Offline","line","off");
    var accary=new Array();
    statement = dDeviceSqlite.dbConnection.createStatement("\
          SELECT aXml FROM td_account WHERE dId = :myVal"); 
    statement.params.myVal =deviceId;
    //var deviceId = null;
    try{
            while(statement.executeStep()){
              accary.push(statement.row.aXml);
              continue;
           }
       }
    finally{
        statement.reset();
    }
    
   Components.utils.import("resource://gre/modules/FileUtils.jsm");
   for(var i=0;i<accary.length;i++)
          { 
            var dbname=accary[i];  
            var dbFile = FileUtils.getFile("ProfD", [dbname]);
            if(dbFile.exists())
            {
              dbFile.remove(true);
            }else{
                continue;
            }
          }   
    statement = dDeviceSqlite.dbConnection.createStatement("\
          DELETE FROM td_account WHERE dId = :myVal");
    statement.params.myVal = deviceId;
    try{
         while(statement.executeStep()){
            continue;
        }
    }
    finally{
        statement.reset();
    }
    
     var menuItem= document.getElementById("connectManagerPanel");
     var panelTree = document.getElementById('folderPaneBox');
     var elem_child = panelTree.childNodes;
     //alert(elem_child.length);
     if(elem_child.length>3){
         for(var i=0;i<elem_child.length;i++){
            if("showDevicesPanelTree"=== elem_child[i].id){
                  elem_child[i].parentNode.removeChild(elem_child[i]);         
            }
         }
         menuItem.setAttribute("checked",true);
         createDevicesListToPanel();
     }
     if(window.opener)
     {    
      var accCP=window.opener.frames["accountCentralPane"];
      var labelV=accCP.document.getElementById('AccountCentralTitle');
      labelV.setAttribute("value","DFuse Mail - Terminal Mail");   
      var displayDesk = window.opener.document.getElementById("displayDeck");
      displayDesk.setAttribute('selectedIndex','0');
     var coll=window.opener.document.getElementById("messagepaneboxwrapper");
     coll.setAttribute('collapsed',true);
     var qfbbar=window.opener.document.getElementById("qfb-show-filter-bar");
     var afbbox=window.opener.document.getElementById("quick-filter-bar");
     afbbox.setAttribute('collapsed',true);
     qfbbar.setAttribute('checked',false);
     qfbbar.setAttribute('disabled',true);
 }else{
     var accCP=window.frames["accountCentralPane"];
     var labelV=accCP.document.getElementById('AccountCentralTitle');
     labelV.setAttribute("value","DFuse Mail - Terminal Mail");
     var displayDesk = document.getElementById("displayDeck");
     displayDesk.setAttribute('selectedIndex','0');
     var coll=document.getElementById("messagepaneboxwrapper");
     coll.setAttribute('collapsed',true);
     var qfbbar=document.getElementById("qfb-show-filter-bar");
     var afbbox=document.getElementById("quick-filter-bar");
     afbbox.setAttribute('collapsed',true);
     qfbbar.setAttribute('checked',false);
     qfbbar.setAttribute('disabled',true);
 }
//    var menuItem = document.getElementById("connectManagerPanel");
//    if(menuItem.getAttribute("checked")){
//     var panelTree = document.getElementById('folderPaneBox');
//     var elem_child = panelTree.childNodes;
//     for(var i=0;i<elem_child.length;i++){
//        if("showDevicesPanelTree" === elem_child[i].id){
//              elem_child[i].parentNode.removeChild(elem_child[i]);
//        }
//     }
//     createDevicesListToPanel();
//    
//    }
    var displayDesk = document.getElementById("displayDeck");
    displayDesk.setAttribute('selectedIndex','0');
    var coll=document.getElementById("messagepaneboxwrapper");
    coll.setAttribute('collapsed',true);
}


function cancelRemoteManagement(deviceName, accountName,aControlType){
    //alert("deviceName:"+deviceName);
    //alert("accountName:"+accountName);
    var statement = dDeviceSqlite.dbConnection.createStatement("\
                     SELECT dId FROM td_device WHERE dName = :myVal");
    statement.params.myVal = deviceName;   
    //alert(deviceName);
    var deviceId = null;
   
     try{
         while(statement.executeStep()){
             deviceId = statement.row.dId;
             continue;
        }
    }
    finally{
        statement.reset();
    }
    
     var statement = dDeviceSqlite.dbConnection.createStatement("\
                     SELECT aId ,aName FROM td_account WHERE dId = :myVal");
    statement.params.myVal =deviceId;
    var accountID = null;
    var aName = null;
     try{
         while(statement.executeStep()){
             aName = statement.row.aName;
             if(aName === accountName)
             {
                accountID = statement.row.aId;
            }
             continue;
        }
    }
    finally{
        statement.reset();
    }
     statement = dDeviceSqlite.dbConnection.createStatement("\
                     UPDATE td_account SET aRemoteStatus = :myStatus, aLastStatus = 3,\
                     aControlType = 0 where aId = :myID");
  
    statement.params.myID      = accountID;
    statement.params.myStatus  = aControlType;
    try{
         while(statement.executeStep()){
             continue;
        }
    }
    finally{
        statement.reset();
    }
    var panelTree = document.getElementById('folderPaneBox');
    var elem_child = panelTree.childNodes;
    for(var i=0;i<elem_child.length;i++){
         if("showDevicesPanelTree" === elem_child[i].id){
             elem_child[i].parentNode.removeChild(elem_child[i]);
         }
    }
    //alert("walk");
    createDevicesListToPanel();
    //alert("jinhai");
}



function createLocalManagement(deviceName, accountName,aControlType){
    var statement = dDeviceSqlite.dbConnection.createStatement("\
                     SELECT dId FROM td_device WHERE dName = :myVal");
    statement.params.myVal = deviceName;   
    //alert(deviceName);
    var deviceId = null;
   
     try{
         while(statement.executeStep()){
             deviceId = statement.row.dId;
             continue;
        }
    }
    finally{
        statement.reset();
    }
    
     var statement = dDeviceSqlite.dbConnection.createStatement("\
                     SELECT aId ,aName,aServiceType FROM td_account WHERE dId = :myVal");
    statement.params.myVal =deviceId;
    var accountID = null;
    var aName = null;
    var aService = null;
     try{
         while(statement.executeStep()){
             aName = statement.row.aName;
             if(aName === accountName)
             {
                accountID = statement.row.aId;
                aService = statement.row.aServiceType;
            }
             continue;
        }
    }
    finally{
        statement.reset();
    }
    
    var statement = dDeviceSqlite.dbConnection.createStatement("UPDATE td_account \
                           SET aLocalStatus = :myStatus, aLastStatus = :myStatus,\
                           aControlType = 1,aServiceType = :myServiceType where aId = :myID");
    statement.params.myID          = accountID;
    statement.params.myStatus      = aControlType;
    statement.params.myServiceType = aService;
    try{
         while(statement.executeStep()){
             continue;
        }
    }
    finally{
        statement.reset();
    }
    statement = dDeviceSqlite.dbConnection.createStatement("SELECT aName, aPwd ,aServiceType FROM td_account WHERE aId = :myID");
    statement.params.myID = accountID;
    try{
         while(statement.executeStep()){
             gLocalAccountInfo.emailAddress = statement.row.aName;
             gLocalAccountInfo.userName = (statement.row.aName).split("@")[0];
             gLocalAccountInfo.passWord = statement.row.aPwd;
             gLocalAccountInfo.recvHostname="pop."+(statement.row.aName).split("@")[1];
             gLocalAccountInfo.sendHostname="smtp."+(statement.row.aName).split("@")[1];
             if((statement.row.aName).split("@")[1]=="163.com")
            {
                gLocalAccountInfo.recvPort="110";
                gLocalAccountInfo.socketType='3';
                
            }else if((statement.row.aName).split("@")[1]=="126.com"){
                
                gLocalAccountInfo.recvPort="110";
                gLocalAccountInfo.socketType='3';
                
            }else if((statement.row.aName).split("@")[1]=="qq.com")
            {
                gLocalAccountInfo.recvPort="995";
                gLocalAccountInfo.socketType='2';
                gLocalAccountInfo.sendPort='465';
                
            }else if((statement.row.aName).split("@")[1]=="sina.com"){
                
                gLocalAccountInfo.recvPort="110";
                gLocalAccountInfo.socketType='1';
                gLocalAccountInfo.sendPort='587';
                 
            }else if((statement.row.aName).split("@")[1]=="gmail.com"){
                gLocalAccountInfo.recvPort="995";
                gLocalAccountInfo.socketType='2';
                gLocalAccountInfo.sendPort='465';
            }else if((statement.row.aName).split("@")[1]=="nfs-hlj.com")
            {
                gLocalAccountInfo.recvPort="995";
                gLocalAccountInfo.socketType='2';
                gLocalAccountInfo.sendPort='465';
                gLocalAccountInfo.recvHostname="pop3.nfschina.com";
                gLocalAccountInfo.sendHostname="smtp.nfschina.com";
            }else{
                gLocalAccountInfo.recvPort="995";
                gLocalAccountInfo.socketType='2';
                gLocalAccountInfo.sendPort='465';
            }
        }
    }
    finally{
        statement.reset();
    }
    var config = new AccountConfig();
    checkIncomingServerAlreadyExists(config);
    config.rememberPassword = true;
    createAccountInBackend(config);
}

function anothertestJSfile()
{
    
    alert("popup");
}

function devRestoreSelectionWithoutContentLoad(tree)
{
  /*  
  if (gRightMouseButtonSavedSelection) {
    let view = gRightMouseButtonSavedSelection.view;
    // restore the selection
    let transientSelection = gRightMouseButtonSavedSelection.transientSelection;
    let realSelection = gRightMouseButtonSavedSelection.realSelection;
    view.selection = realSelection;
    // replay any calls to adjustSelection, this handles suppression.
    transientSelection.replayAdjustSelectionLog(realSelection);
    gRightMouseButtonSavedSelection = null;

    if (tree)
      tree.treeBoxObject.invalidate();
     
    alert("look at me!");
    
    UpdateMailToolbar("RestoreSelectionWithoutContentLoad");
  }
    */
  //alert("please ");
}

function devFolderPaneOnPopupHiding()
{
  //alert("调用devRestoreSelectionWithoutContentLoad");
  devRestoreSelectionWithoutContentLoad(document.getElementById("showDevicesPanelTree"));
  //alert("完成RestoreSelectionWithoutContentLoad调用");
}

function backupStartOrStop()
{
    var menu = document.getElementById("mobileDevicePaneContext").getElementsByTagName('menu');
    var menuSets = document.getElementById("mobileDevicePaneContext").getElementsByTagName('menuitem');
    var localbackbut=document.getElementById("backul");
    var setbackupopr=document.getElementById("bakupSet");
    if(setbackupopr.getAttribute("checked"))
    {     
     localbackbut.setAttribute('checked',false);
     localbackbut.setAttribute('disabled',false);    

    }else{
        
    localbackbut.setAttribute('checked',false);
    localbackbut.setAttribute('disabled',true);
    
    }
    for(var i=0;i< menu.length;i++)
    {
        if(menu[i].id === "deviceInformationBackup" )
        {
            var disableAttribute = menu[i].getAttribute("disabled");
            if(disableAttribute === "false")
                menu[i].setAttribute("disabled","true");
            else if(disableAttribute === "true")
                menu[i].setAttribute("disabled","false");
        }
    }
    
    for(var i=0;i< menuSets.length;i++)
    {
         if(menuSets[i].id === "backupMailFolderInformation" )
         {
            var disableAttribute = menuSets[i].getAttribute("disabled");
            if(disableAttribute === "false")
                menuSets[i].setAttribute("disabled","true");
            else if(disableAttribute === "true")
                menuSets[i].setAttribute("disabled","false");
         }
         else if(menuSets[i].id === "currentDevieInformationBackup" )
         {
            var disableAttribute = menuSets[i].getAttribute("disabled");
            if(disableAttribute === "false")
                menuSets[i].setAttribute("disabled","true");
            else if(disableAttribute === "true")
                menuSets[i].setAttribute("disabled","false");
         }
    }
}

function syncStartOrStop()
{
    var menu = document.getElementById("mobileDevicePaneContext").getElementsByTagName('menu');
    var localbackbut=document.getElementById("getmt");
    var setbackupopr=document.getElementById("syncSet");
    if(setbackupopr.getAttribute("checked"))
    {     
     localbackbut.setAttribute('checked',false);
     localbackbut.setAttribute('disabled',false);    
     
    }else{
        
    localbackbut.setAttribute('checked',false);
    localbackbut.setAttribute('disabled',true);
    
    }
    for(var i=0;i< menu.length;i++)
    {
        if(menu[i].id === "accountDataSync" )
        {
            var disableAttribute = menu[i].getAttribute("disabled");
            if(disableAttribute === "false")
            {
                 menu[i].setAttribute("disabled","true");
            }
            else if(disableAttribute === "true")
            {
                 menu[i].setAttribute("disabled","false");
             }
        }
         if(menu[i].id === "syncMailFolderData")
        {
            var disableAttribute = menu[i].getAttribute("disabled");
            if(disableAttribute === "false")
                menu[i].setAttribute("disabled","true");
            else if(disableAttribute === "true")
                menu[i].setAttribute("disabled","false");
        }
    }
    return true;
}


function devFillFolderPaneContextMenu()
{
  var menuSets=document.getElementById("mobileDevicePaneContext").getElementsByTagName('menuitem');
  var menuSeparator = document.getElementById("mobileDevicePaneContext").getElementsByTagName('menuseparator');
  var menu = document.getElementById("mobileDevicePaneContext").getElementsByTagName('menu');
  var listIds=[];
  listIds=DevicePaneSelectionMenu();
  //alert(listIds);
  var devicesAccountsArr = getDeviceAccountsMenu();
  var devicesArr = getDevicesNameInfo();
//  for(var i =0 ;i < devicesArr.length;i++)
//  {
//      alert(devicesArr[i]);
//  }
//  for(var i=0;i<devicesAccountsArr.length;i++){
//       alert("devicesAccountsArr.length :"+devicesAccountsArr.length);
//      for(var j=0;j<devicesAccountsArr[i].length;j++){
//          alert("devicesAccountsArr[i].length :"+devicesAccountsArr[i].length + "i="+i);
//          (devicesAccountsArr[i][j]);
//          alert(devicesAccountsArr[i]);
//      }
//  }

  var numMailFolder = listIds.split("_")[2];
  if(numMailFolder)
  {
        var mailFolder = numMailFolder.substr(1,numMailFolder.length);
        //alert(mailFolder);
  }
  //getSelectedTreeItemIds();
//alert(listIds);//.substr(0,3)
//alert(listIds.split("_")[0]);
//alert(listIds.split("_")[1]);
   if(listIds==="")
   {
        for(var i=0;i< menu.length;i++)
        {
            if(menu[i].id === "deviceInformationBackup")
            {
                //alert(menu[i].id);
                var menupop = document.getElementById("two");
                var list = menupop.childElementCount;
                for(var t = 0; t < list;t++)
                {
                    menupop.removeChild(menupop.children[0]);
                }
                var accountNum = devicesArr.length;
                for(var j = 0; j <accountNum;j++)
                {
                    var item = document.createElement("menuitem");
                    var itemId = "Device1_" + j;
                    item.setAttribute("id",itemId);
                    var accountName = devicesArr[j];
                    //alert(accountName);
                    item.setAttribute("label",accountName);
                    item.onclick = function(event){ 
                        BackUpSelDevInFo(event);
                    };
                    menupop.appendChild(item);
                }
                 var lastitem = document.createElement("menuitem");
                 var alldevId= "Device1_" +accountNum;
                  lastitem.setAttribute("id",alldevId);
                  lastitem.setAttribute("label","所有设备");
                  lastitem.onclick = function(){ 
                  BackUpALLDeviceInfo();
                   };
                menupop.appendChild(lastitem);
                ShowMenuItem(menu[i].id, true);  
            }
            else
            {
                ShowMenuItem(menu[i].id, false);
            }
        }
        for(i=0;i< menuSeparator.length;i++)
        {
         
            ShowMenuItem(menuSeparator[i].id, false);
        }
       
        for(i=0;i< menuSets.length;i++)
        {
           
            if(menuSets[i].id === "terminalDeviceManagement")
            {
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id === "checkDeviceInformation")
            {
                menuSets[i].onclick = function(){ 
                checkAllDevice();
                    };
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id.split("_")[0] === "Device1")
            {
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id.split("_")[0] === "Device2")
            {
                ShowMenuItem(menuSets[i].id, true);
            }
            else
            {
                ShowMenuItem(menuSets[i].id, false);
            }
        
        }
       
   }
   else if(listIds.split("_")[0] === "Account" && mailFolder.length === 0)
   {
       
       for(i=0;i< menu.length;i++)
        {
            if(menu[i].id ==="syncMailFolderData")
            {
                ShowMenuItem(menu[i].id, true);
              //  menu[i].setAttribute("disabled","true");
            }
            else
            {
               ShowMenuItem(menu[i].id, false);
            }
        }
        for(i=0;i< menuSeparator.length;i++)
        {
            ShowMenuItem(menuSeparator[i].id, false);
        }
        
        for(i=0;i< menuSets.length;i++)
        {
            if(menuSets[i].id === "checkAccountInformation")
            {
                var accountname = devicesAccountsArr[listIds.split("_")[1]][listIds.split("_")[2]];
                var devicename = devicesArr[listIds.split("_")[1]];
                menuSets[i].onclick = function(){
                    checkOneAccount(accountname,devicename);
                };
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id === "cancelRemoteManagement")
            {
                var accountname = devicesAccountsArr[listIds.split("_")[1]][listIds.split("_")[2]];
                var devicename = devicesArr[listIds.split("_")[1]];
              
                menuSets[i].onclick = function(){
                    cancelRemoteManagement(devicename,accountname,0);
                };
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id === "startLocalManagement")
            {
                var accountname = devicesAccountsArr[listIds.split("_")[1]][listIds.split("_")[2]];
                var devicename = devicesArr[listIds.split("_")[1]];
                menuSets[i].onclick = function(){
                    createLocalManagement(devicename,accountname,0);
                };
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id === "syncRecvFolder")
            {
                var accountname = devicesAccountsArr[listIds.split("_")[1]][listIds.split("_")[2]];
                var devicename = devicesArr[listIds.split("_")[1]];
                menuSets[i].onclick = function(){
                    syncMailFolder(devicename,accountname,"recv");
                };
                ShowMenuItem(menuSets[i].id, true);              
            }
            else if(menuSets[i].id === "syncSendFolder")
            {
                var accountname = devicesAccountsArr[listIds.split("_")[1]][listIds.split("_")[2]];
                var devicename = devicesArr[listIds.split("_")[1]];
                menuSets[i].onclick = function(){
                    syncMailFolder(devicename,accountname,"send");
                };
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id === "syncDraftFolder")
            {
                var accountname = devicesAccountsArr[listIds.split("_")[1]][listIds.split("_")[2]];
                var devicename = devicesArr[listIds.split("_")[1]];
                menuSets[i].onclick = function(){
                    syncMailFolder(devicename,accountname,"draft");
                };
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id === "backupMailFolderInformation")
            {
                
                var accountname = devicesAccountsArr[listIds.split("_")[1]][listIds.split("_")[2]];
                var devicename = devicesArr[listIds.split("_")[1]];
                menuSets[i].onclick=function(){                
                    BackUpCurAccInFo(accountname,devicename);   
                };
                ShowMenuItem(menuSets[i].id, true);
            }
            else
            {
                ShowMenuItem(menuSets[i].id, false);
            }            
         }
   }
   else if(listIds.split("_")[0] === "Device")
   {
       
        for(i=0;i< menu.length;i++)
        { 
            if(menu[i].id ==="accountDataSync")
            {   
                var menupop = document.getElementById("one");
                var list = menupop.childElementCount;
                for(var t = 0; t < list;t++)
                {
                    menupop.removeChild(menupop.children[0]);
                }
                /*FUCK*/
                var deviceNum = listIds.split("_")[1];
                var accountNum = devicesAccountsArr[deviceNum].length;
                for(var j = 0; j < accountNum;j++)
                {
                    var item = document.createElement("menuitem");
                    var itemId = "Account_" + j;
                    item.setAttribute("id",itemId);
                    var accountName = devicesAccountsArr[deviceNum][j];
                    item.setAttribute("label",accountName);
                    menupop.appendChild(item);
                }
                for(var j = 0; j < accountNum;j++)
                {
                    var itemId =  "Account_" + j;
                    var itemObject = document.getElementById(itemId);
                    itemObject.onclick = function(event)
                    {
                        var accountname = event.target.getAttribute("label");
                        var devicename = devicesArr[listIds.split("_")[1]];
                        syncMailFolder(devicename,accountname,"all");
                    };
                }
                ShowMenuItem(menu[i].id, true);
            }
            else
            {
                 ShowMenuItem(menu[i].id, false);
            }
        }
        for(i=0;i< menuSeparator.length;i++)
        {
            ShowMenuItem(menuSeparator[i].id, false);
        }
        
        for(i=0;i< menuSets.length;i++)
        {
            if(menuSets[i].id === "logoutDevice")
            {   
                   var name = devicesArr[listIds.split("_")[1]];
                 /*fuck*/
                    //alert("dev"+name);
                    menuSets[i].onclick = function(){
                    logoutDevice1(name);
                };
                ShowMenuItem(menuSets[i].id, true);
            }  
            else  if(menuSets[i].id === "deviceCheckDeviceInformation")
            {
                var name = devicesArr[listIds.split("_")[1]];
                //alert(name);
                menuSets[i].onclick = function(){ 
                checkOneDevice(name);
                    };
                ShowMenuItem(menuSets[i].id, true);
            }  
            else  if(menuSets[i].id.split("_")[0] === "Account")
            {
                ShowMenuItem(menuSets[i].id, true);
            }  
            else if(menuSets[i].id === "itemId")
            {
                ShowMenuItem(menuSets[i].id, true);
            }
            else if(menuSets[i].id === "currentDevieInformationBackup")
            {
                var name = devicesArr[listIds.split("_")[1]];
                menuSets[i].onclick = function(){ 
                BackUpCurDevInFo(name);
                    };
                ShowMenuItem(menuSets[i].id, true);
            }
            else
            {
                 ShowMenuItem(menuSets[i].id, false);
            }
         }     
   }
   else if(mailFolder === "send")
   {
      
        for(i=0;i< menu.length;i++)
        {
         
            ShowMenuItem(menu[i].id, false);
        }
        for(i=0;i< menuSeparator.length;i++)
        {
            ShowMenuItem(menuSeparator[i].id, false);
        }
        
        for(i=0;i< menuSets.length;i++)
        {
            if(menuSets[i].id === "send")
            {
                ShowMenuItem(menuSets[i].id, true);
            }
            else
            {
                ShowMenuItem(menuSets[i].id, false);
            }  
         }     
   }
   else if(mailFolder === "recv")
   {
       for(i=0;i< menu.length;i++)
        {
         
            ShowMenuItem(menu[i].id, false);
        }
     
        for(i=0;i< menuSeparator.length;i++)
        {
            ShowMenuItem(menuSeparator[i].id, false);
        }
        
        for(i=0;i< menuSets.length;i++)
        {
            if(menuSets[i].id === "recv")
            {
                ShowMenuItem(menuSets[i].id, true);
            }
            else
            {
                ShowMenuItem(menuSets[i].id, false);
            }  
         }     
   }
   else if(mailFolder === "draft")
   {
      
        for(i=0;i< menu.length;i++)
        {
         
            ShowMenuItem(menu[i].id, false);
        }
        for(i=0;i< menuSeparator.length;i++)
        {
            ShowMenuItem(menuSeparator[i].id, false);
        }
        
        for(i=0;i< menuSets.length;i++)
        {
            if(menuSets[i].id === "draft")
            {
                ShowMenuItem(menuSets[i].id, true);
            }
            else
            {
                ShowMenuItem(menuSets[i].id, false);
            }  
         }     
   } 
  return true;
}


function ShowMenuItem(id, showItem)
{
  document.getElementById(id).hidden = !showItem;
}

function EnableMenuItem(id, enableItem)
{
  document.getElementById(id).disabled = !enableItem;
}

function SetMenuItemLabel(id, label)
{
  document.getElementById(id).setAttribute('label', label);
}

// helper function used by shouldShowSeparator
function hasAVisibleNextSibling(aNode)
{
  var sibling = aNode.nextSibling;
  while (sibling)
  {
    if (sibling.getAttribute("hidden") !== "true"
        && sibling.localName !== "menuseparator")
      return true;
    sibling = sibling.nextSibling;
  }
  return true;
}


/*备份选中的设备信息*/
function BackUpSelDevInFo(event)
{
    //alert("me");
    var devName=event.target.getAttribute("label");
    dDeviceSqlite.onLoad();
    var statement = dDeviceSqlite.dbConnection.createStatement("\
          SELECT dName,dLocation,dUDN FROM td_device WHERE dName = :myVal"); 
    statement.params.myVal =devName;
    //var deviceId = null;
    try{
            while(statement.executeStep()){
                    var httpHead = statement.row.dLocation.split("/")[0];
                   var ipAndPort = statement.row.dLocation.split("/")[2];
                   var data ="设备名："+ statement.row.dName+"\n"+"设备IP地址："+httpHead+"\/\/"+ipAndPort+"\n"+"设备唯一标识："+statement.row.dUDN;
               continue;
           }
       }
    finally{
        statement.reset();
    }
    var driName=devName;
    var fileName=devName+".txt";
    backUpToFile(driName,fileName,data);
}
/*备份当前设备信息*/
function BackUpCurDevInFo(name)
{
    //alert("么么达");
    dDeviceSqlite.onLoad();
   var statement = dDeviceSqlite.dbConnection.createStatement("\
          SELECT dName,dLocation,dUDN FROM td_device WHERE dName = :myVal"); 
    statement.params.myVal =name;
    //var deviceId = null;
    try{
            while(statement.executeStep()){
                    var httpHead = statement.row.dLocation.split("/")[0];
                   var ipAndPort = statement.row.dLocation.split("/")[2];
                   var data ="设备名："+ statement.row.dName+"\n"+"设备IP地址："+httpHead+"\/\/"+ipAndPort+"\n"+"设备唯一标识："+statement.row.dUDN;
               continue;
           }
       }
    finally{
        statement.reset();
    }
    var driName=name;
    var fileName=name+".txt";
    backUpToFile(driName,fileName,data);
}
/*备份所有设备信息*/
function BackUpALLDeviceInfo()
{
    dDeviceSqlite.onLoad();
    var statement = dDeviceSqlite.dbConnection.createStatement("\
          SELECT dName,dLocation,dUDN FROM td_device WHERE dHandleStatus = :myVal"); 
    statement.params.myVal =1;
    var data = "";
    try{
            while(statement.executeStep()){
                    var httpHead = statement.row.dLocation.split("/")[0];
                   var ipAndPort = statement.row.dLocation.split("/")[2];
                   var data =data+"设备名："+ statement.row.dName+"\n"+"设备IP地址："+httpHead+"\/\/"+ipAndPort+"\n"+"设备唯一标识："+statement.row.dUDN+"\n"+"____________________________________________________________________________"+"\n";
               continue;
           }
       }
    finally{
        statement.reset();
    }
    var driName="ALLDEVINFO";
    var fileName="ALLDEVINFO"+".txt";
    backUpToFile(driName,fileName,data);
}
/*备份当前邮件账户信息*/
function BackUpCurAccInFo(accname,devname)
{
    //alert("么么达");
    //alert(accname+":"+devname);
    dDeviceSqlite.onLoad();
   var statement = dDeviceSqlite.dbConnection.createStatement("\
          SELECT dId FROM td_device WHERE dName = :myVal"); 
    statement.params.myVal =devname;
    //var deviceId = null;
    try{
            while(statement.executeStep()){
                  var deviceId=statement.row.dId;
               continue;
           }
       }
    finally{
        statement.reset();
    }
        //alert(deviceId);
  var statement = dDeviceSqlite.dbConnection.createStatement("\
          SELECT aName,aPwd,aXml,aServiceType FROM  td_account WHERE dId = :myVal and aName = :myVal1"); 
      statement.params.myVal =deviceId;
      statement.params.myVal1 =accname;
    //var deviceId = null;
    //alert("aaaaa");
    try{
            while(statement.executeStep()){
                //alert("bbbbb");
                var emailTY=accname.split("@")[1];
                //alert(emailTY);
                var data ="邮件账户："+ statement.row.aName+"\n"+"密码："+statement.row.aPwd+"\n"+"存储数据库名："+statement.row.aXml.split("_")[0]+"\n"+"收件服务器："+statement.row.aServiceType+"."+emailTY+"\n"+"发件服务器："+"smtp."+emailTY+"\n";
               //alert(data);
                continue;
           }
       }
    finally{
        statement.reset();
    }
    //alert(data);
    var driName=devname;
    //alert(1);
    var fileName=accname.split("@")[0]+".txt";
    //alert(dirName+"="+fileName);
    backUpToFile(driName,fileName,data);
}
 
//var data ="邮件账户："+ statement.row.dName+"\n"+"密码："+ +"\n"+"存储数据库名："+statement.row.dUDN+"\n"+"发件服务器"+ +"\n"+"收件服务器";